﻿using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Microsoft.TeamFoundation.TestManagement.Client;
using TestStepsEditor.Viewers;
using TestStepsEditor.Viewers.Data;

namespace TestStepsEditor.Actions
{
    internal class TestCaseAttachments
    {
        public static BindingList<AttachmentViewer> Load(SharedData data)
        {
            var attachmentViewer = new BindingList<AttachmentViewer>();
            if (data.GridView.CurrentRow != null)
            {
                if (data.GridView.CurrentRow.Cells["Attachment"].Value != null)
                    foreach (
                        ITestAttachment attachment in
                            data.GridView.CurrentRow.Cells["Attachment"].Value as IAttachmentCollection)
                    {
                        //Download each file locally so that the user can edit if they need to.
                        string directoryName = @"C:\TestCaseEditor\UploadFiles";
                        if (!Directory.Exists(directoryName))
                            Directory.CreateDirectory(@"C:\TestCaseEditor\UploadFiles");
                        string filename = directoryName + "\\" + attachment.Name;
                        attachment.DownloadToFile((filename));

                        //Add the attachment to the viewer.
                        attachmentViewer.Add(new AttachmentViewer(attachment.Name, attachment.Comment, filename,
                                                                  data.GridView.CurrentRow.Index));
                    }
            }
            return attachmentViewer;
        }

        public static BindingList<AttachmentViewer> Add(SharedData data, BindingList<AttachmentViewer> attachmentViewer)
        {
            var ofd = new OpenFileDialog();
            ofd.ShowDialog();
            if (ofd.FileName.Count() > 0)
            {
                attachmentViewer.Add(new AttachmentViewer(ofd.SafeFileName, "", ofd.FileName,
                                                          data.GridView.CurrentRow.Index));
            }
            return attachmentViewer;
        }

        public static BindingList<AttachmentViewer> Remove(DataGridView gridView,
                                                           BindingList<AttachmentViewer> attachmentViewer)
        {
            if (gridView.SelectedRows.Count > 0)
            {
                foreach (DataGridViewRow row in gridView.SelectedRows)
                {
                    if (row != null)
                    {
                        attachmentViewer.RemoveAt(gridView.Rows.IndexOf(row));
                        gridView.Focus();
                    }
                }
            }
            else
            {
                if (gridView.CurrentRow != null)
                {
                    attachmentViewer.RemoveAt(gridView.CurrentRow.Index);
                    gridView.Focus();
                }
            }
            return attachmentViewer;
        }

        public static SharedData Save(SharedData data, BindingList<AttachmentViewer> attachmentViewer)
        {
            if (attachmentViewer.Count() > 0)
            {
                if (data.StepsCollection[data.GridView.CurrentRow.Index].Attachment != null)
                    data.StepsCollection[data.GridView.CurrentRow.Index].Attachment.Clear();
                else
                {
                    data.StepsCollection[data.GridView.CurrentRow.Index].Attachment = new AttachmentCollection();
                }

                foreach (AttachmentViewer attachment in attachmentViewer)
                {
                    if (attachment.FileLocation != "")
                    {
                        ITestAttachment file;
                        if (data.TestWorkItem.Actions.Count > 0)
                        {
                            file =
                                (data.TestWorkItem.Actions[data.GridView.CurrentRow.Index] as ITestStep).
                                    CreateAttachment(
                                        attachment.FileLocation);
                            file.Comment = attachment.Comment;
                        }
                        else
                        {
                            data.TestWorkItem.Actions.Add(data.TestWorkItem.CreateTestStep());
                            {
                                file =
                                    (data.TestWorkItem.Actions[data.GridView.CurrentRow.Index] as ITestStep).
                                        CreateAttachment(
                                            attachment.FileLocation);
                                file.Comment = attachment.Comment;
                            }
                        }
                        data.StepsCollection[data.GridView.CurrentRow.Index].Attachment.Add(file);

                        data = InsertImages.AddImageToGrid(data);
                    }
                }
            }
            else
            {
                data.StepsCollection[data.GridView.CurrentRow.Index].Attachment.Clear();
            }
            return data;
        }

        public static void Edit(DataGridView gridView)
        {
            if (gridView.CurrentRow != null)
            {
                var psi = new ProcessStartInfo();
                psi.FileName = gridView.CurrentRow.Cells["FileLocation"].Value.ToString();
                var p = new Process();
                p.StartInfo = psi;
                p.Start();
            }
        }
    }
}